package com.jason.GUI;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;

import com.jason.Errors.ShowErrors;
import com.jason.Gmail.Connection.InboxReader;
import com.jason.Sound.WavPlayer;

public class Main extends JFrame {

	private JPanel contentPane;
	private JTextField textUsername;
	private JPasswordField textPassword;
	private JLabel lblUsername;
	private JLabel lblPass;
	private JButton btnStart;
	private BtnStartListener startListener = new BtnStartListener();
	private ShowErrors error = new ShowErrors();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		
		this.setTitle("SWTOR E-Mail Watch - Beta 1");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{115, 321, 0};
		gbl_contentPane.rowHeights = new int[]{52, 43, 48, 43, 35, 25, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		lblUsername = new JLabel("Username:");
		GridBagConstraints gbc_lblUsername = new GridBagConstraints();
		gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
		gbc_lblUsername.anchor = GridBagConstraints.EAST;
		gbc_lblUsername.gridx = 0;
		gbc_lblUsername.gridy = 1;
		contentPane.add(lblUsername, gbc_lblUsername);
		
		textUsername = new JTextField();
		GridBagConstraints gbc_textUsername = new GridBagConstraints();
		gbc_textUsername.fill = GridBagConstraints.BOTH;
		gbc_textUsername.insets = new Insets(0, 0, 5, 0);
		gbc_textUsername.gridx = 1;
		gbc_textUsername.gridy = 1;
		contentPane.add(textUsername, gbc_textUsername);
		textUsername.setColumns(10);
		
		lblPass = new JLabel("Pass:");
		GridBagConstraints gbc_lblPass = new GridBagConstraints();
		gbc_lblPass.insets = new Insets(0, 0, 5, 5);
		gbc_lblPass.anchor = GridBagConstraints.EAST;
		gbc_lblPass.gridx = 0;
		gbc_lblPass.gridy = 2;
		contentPane.add(lblPass, gbc_lblPass);
		
		textPassword = new JPasswordField();
		GridBagConstraints gbc_textPassword = new GridBagConstraints();
		gbc_textPassword.fill = GridBagConstraints.BOTH;
		gbc_textPassword.insets = new Insets(0, 0, 5, 0);
		gbc_textPassword.gridx = 1;
		gbc_textPassword.gridy = 2;
		contentPane.add(textPassword, gbc_textPassword);
		
		btnStart = new JButton("Start");
		GridBagConstraints gbc_btnStart = new GridBagConstraints();
		gbc_btnStart.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnStart.gridwidth = 2;
		gbc_btnStart.anchor = GridBagConstraints.NORTH;
		gbc_btnStart.gridx = 0;
		gbc_btnStart.gridy = 5;
		contentPane.add(btnStart, gbc_btnStart);
		
		btnStart.addActionListener(startListener);
	}
	
	class BtnStartListener implements ActionListener{

		
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
			String userName = textUsername.getText();
			char[] pass = textPassword.getPassword();
			String s_pass = "";
			for (char p : pass){
				s_pass += p;
			}
			
			InboxReader ir = new InboxReader();
			ir.setUserName(userName); ir.setPassword(s_pass);
			
			do {
				ir.connect();
				ir.findEmail("swtor");
				try {
					Thread.sleep(10 * 1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					error.display(e);
				}
			} while (!ir.getSuccess());
			if (ir.getSuccess()){
				new WavPlayer("Welcome.wav").run();
				String title = "I CANT BELIEVE IT!";
				String message = "YOU GOT A MESSAGE\nOMG YOU CAN PLAY! (maybe)";
				JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
			}
		}
		
	}
}
